package net.bungaeting.client.android.request;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import net.bungaeting.client.android.utils.JsonUtil;

import android.os.Environment;
import android.util.Log;

public class LocalFileRequestor extends Requestor {
	private String externalFilePath;
	public LocalFileRequestor() {
		externalFilePath = Environment.getExternalStorageDirectory().getAbsolutePath();
	}
	private String getFileContents(String fileName){
		BufferedReader reader = null;
		String contents = "";
		try {
			reader = new BufferedReader(new FileReader(fileName));
			
			String line = reader.readLine();
			while (line != null){
				contents += line;
				line = reader.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
		finally{
			if (reader != null)
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return contents;
	}
	@Override
	public List<Response> doRequest(Request request) {
		
		String fileName = this.getServiceFileName(request.getServiceId());
		Log.i("TEST", "fileName " + fileName);
		Map<String, Object> mapResponse = JsonUtil.toMap(getFileContents(fileName));
//		return createResponse(request, mapResponse);
		return null;
	}

	private String getServiceFileName(String serviceId){
		return externalFilePath + "/data/bungaeting/" + serviceId + ".response.json";
	}
}
